//
//  SCNRankingHeaderView.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/5/15.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNRankingHeaderView: UIView {
    
    typealias segmentedAction = (Int) -> Void
    
    var segmentedActionBlock: segmentedAction?
    
    var segmentedIndex: Int {
        set {
            segmentedCtrl.selectedSegmentIndex = newValue
            if newValue == 0  {
                moneyL.text = "累计投资(元)"
                hintL.snp.updateConstraints({ (make) in
                    make.height.equalTo(0)
                })
            }else {
                moneyL.text = "目前金币"
                hintL.snp.updateConstraints({ (make) in
                    make.height.equalTo(30)
                })
            }
        }
        get {
            return self.segmentedIndex
        }
    }
    
    

    lazy var segmentedCtrl: UISegmentedControl = {
        let seg = UISegmentedControl.init(items: ["投资排行榜","金币排行榜"])
        
        seg.addTarget(self, action: #selector(segmentedCtrlAction(_:)), for: .valueChanged)
        
        return seg
    }()
    lazy var link: UIView = {
        let view = UIView()
        view.backgroundColor = SCNConstants.colorWith255RGBA(230, g: 230, b: 230, a: 1)
        return view
    }()
    lazy var hintL: UILabel = {
       
        let lab = UILabel()
        lab.text = "提示：兑换礼品会消耗金币，影响排名"
        lab.textColor = SCNConstants.colorWith255RGBA(37, g: 136, b: 229, a: 1)
        lab.font = UIFont.systemFont(ofSize: 14)
        return lab
    }()
    
    lazy var titleView: UIView = {
       
        let view = UIView()
        view.backgroundColor = SCNConstants.colorWith255RGBA(230, g: 230, b: 230, a: 1)
        return view
     }()
    
    lazy var gradeL: UILabel = {
        let lab = UILabel()
        lab.text = "排行"
        lab.textAlignment = .center
        lab.font = UIFont.systemFont(ofSize: 14)
        return lab
    }()
    
    lazy var mobileL: UILabel = {
        let lab = UILabel()
        lab.text = "用户名"
        lab.textAlignment = .center
        lab.font = UIFont.systemFont(ofSize: 14)
        return lab
    }()
    
    lazy var moneyL: UILabel = {
        let lab = UILabel()
        lab.text = "累计投资(元)"
        lab.textAlignment = .center
        lab.font = UIFont.systemFont(ofSize: 14)
        return lab
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(segmentedCtrl)
        addSubview(link)
        addSubview(hintL)
        addSubview(titleView)
        titleView.addSubview(gradeL)
        titleView.addSubview(mobileL)
        titleView.addSubview(moneyL)
        
        segmentedCtrl.snp.makeConstraints { (make) in
            make.leading.equalTo(20)
            make.top.equalTo(15)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 40, height: 35))
        }
        link.snp.makeConstraints { (make) in
            make.leading.equalTo(0)
            make.top.equalTo(65)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH, height: 0.5))
        }
        hintL.snp.makeConstraints { (make) in
            make.leading.equalTo(10)
            make.top.equalTo(65)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20, height: 0))
        }
        titleView.snp.makeConstraints { (make) in
            make.leading.equalTo(0)
            make.top.equalTo(hintL.snp.bottom).offset(0)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH, height: 35))
        }
        gradeL.snp.makeConstraints { (make) in
            make.leading.equalTo(0)
            make.top.equalTo(0)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        mobileL.snp.makeConstraints { (make) in
            make.leading.equalTo(80)
            make.top.equalTo(0)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        moneyL.snp.makeConstraints { (make) in
            make.leading.equalTo(180)
            make.top.equalTo(0)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 180, height: 35))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func segmentedCtrlAction(_ sender: UISegmentedControl) {

        segmentedActionBlock?(sender.selectedSegmentIndex)
    }
}
